import com.wonders.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

public class Md5Test {
    public static void main(String[] args) {
        DefaultSecurityManager sc = new DefaultSecurityManager();

        CustomRealm customRealm = new CustomRealm();

        // 设置自定义realm的凭证匹配器：使用哈希匹配
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        // 设置使用哪种hash算法：md5
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        // 设置散列多少次：1024次
        hashedCredentialsMatcher.setHashIterations(1024);
        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        sc.setRealm(customRealm);

        SecurityUtils.setSecurityManager(sc);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("maomao", "123");

        try {
            subject.login(token);
            System.out.println("认证通过：" + subject.isAuthenticated());
        } catch (UnknownAccountException e) {
            System.out.println("用户名错误");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
        }
    }
}
